# ====================================================================
#
# This .bash_aliases has more than just alias, but since most .bashrc
# files use that default convention, put them here.
#
# Ordered by:
#
#    0. interactive check
#    1. history
#    2. var checks
#    3. paths
#    4. other vars
#    5. aliases
#    6. completion
#    7. private
#
# See also:
#
#    - bash(1)
#    - /usr/share/doc/bash/examples/startup-files
#    - /etc/bash.bashrc
#    - /etc/profile
#
# ====================================================================


# ========== INTERACTIVE CHECKS ======================================
case $- in
    *i*) ;;
      *) return;;
esac


# ========== HISTORY =================================================
# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth

# append to the history file, don't overwrite it
shopt -s histappend

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000


# ========== VAR CHECKS ==============================================

if [ -z "$HOME" ]; then
    echo "WARNING: \$HOME not set"
fi


if [ -z "$PATH" ]; then
    echo "WARNING: \$PATH not set"
fi


shopt -s checkwinsize


# ========== PATHS ===================================================

if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi


if [ -d "$HOME/.local/bin" ] ; then
    PATH="$HOME/.local/bin:$PATH"
fi


# ========== OTHER VARS ==============================================

hash emacs && \
    export EDITOR="emacs -nw"


export LANG=en_US.UTF-8
export LANGUAGE=en_US.UTF-8
export LC_ALL=en_US.UTF-8


if [ -d "$HOME/.cargo" ]; then
    source "$HOME/.cargo/env"
fi


if [ -d "$HOME/.nvm" ]; then
    export NVM_DIR="$HOME/.nvm"
    [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
    [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion
fi


if [ -d "$HOME/.pyenv" ]; then
    PYENV_ROOT="$HOME/.pyenv"
    PATH="$PYENV_ROOT/bin:$PYENV_ROOT/shims:$PATH"
    eval "$(pyenv init -)"
fi


if [ -d "$HOME/.sdkman" ]; then
    export SDKMAN_DIR="$HOME/.sdkman"
    [[ -s "$HOME/.sdkman/bin/sdkman-init.sh" ]] && \
        source "$HOME/.sdkman/bin/sdkman-init.sh"
fi


function set_win_title(){
    echo -ne "\033]0; $(basename "$PWD") \007"
}
hash starship && \
    starship_precmd_user_func="set_win_title" && \
    export STARSHIP_CONFIG=~/.starship/config.toml && \
    eval "$(starship init bash)"


# ========== ALIASES =================================================

if [ -x /usr/bin/dircolors ]; then
    test -r ~/.dircolors && \
        eval "$(dircolors -b ~/.dircolors)" || \
            eval "$(dircolors -b)"
    alias ls="ls --color=auto"
    alias grep="grep --color=auto"
fi
alias ll="ls -l"


alias bash-reload="source ~/.bashrc"
alias grails='echo ""; grails'
alias irc-goto-screen="screen -r irssi"
alias irc-init-screen="screen -dmS irssi"
alias irc="irc-init-screen; irc-goto-screen"
alias vlc-no-video="cvlc --play-and-exit --no-video"


# NOTE: Deprecated aliases, pending removal.
alias gowork="cd ~/workspace"
alias initgit='git config --global core.editor "emacs -nw"'
alias nodo="sudo -k"
alias vxandr="~/Downloads/android-studio/bin/studio.sh"
alias vxdown="youtube-dl -o \"%(title)s.%(ext)s\" "
alias vxedit="emacs -q -nw ~/.bashrc"
alias vxhpro="emacs -q -nw ~/.emacs.d/projectile-bookmarks.eld"
alias vxtest='echo "vxtest_me"; nodo'
alias vxvc='/usr/bin/git --git-dir=$HOME/.vxvc/ --work-tree=$HOME'


# ========== COMPLETION ==============================================
if ! shopt -oq posix; then
  if [ -f /usr/share/bash-completion/bash_completion ]; then
    . /usr/share/bash-completion/bash_completion
  elif [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
  fi
fi


# ========== PRIVATE =================================================

if [ -f "$HOME/.bash_aliases" ]; then
    source "$HOME/.bash_aliases"
fi


if [ -f "$HOME/.bash_aliases_pvt" ]; then
    source "$HOME/.bash_aliases_pvt"
fi
